package android.slc.extras.component.attchment.domain;

import android.slc.appbase.R;
import android.slc.appbase.api.slc.callback.SlcObserver;
import android.slc.appbase.api.slc.callback.po.SlcEntity;
import android.slc.appbase.ui.vd.BaseListVmBox;
import android.slc.attachment.bean.Progress;
import android.slc.extras.component.attchment.entity.Attachment;
import android.slc.extras.component.attchment.entity.StorageForm;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableBoolean;

import com.blankj.utilcode.util.CollectionUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.kunminx.architecture.ui.callback.ProtectedUnPeekLiveData;
import com.kunminx.architecture.ui.callback.UnPeekLiveData;

import java.util.List;

import io.reactivex.rxjava3.core.Observable;

public class AttachmentReviewVmBox<T extends AttachmentReviewVmBox.AttachmentAdapter> extends BaseListVmBox<Attachment> {
    protected Long[] ids;//关联的id
    private List<StorageForm> storageFormList;
    public final ObservableBoolean showAttEmptyHint = new ObservableBoolean(true);
    protected final UnPeekLiveData<Attachment> notifyAttItemDataChangeLd = new UnPeekLiveData<>();
    private T attachmentAdapter;

    public AttachmentReviewVmBox() {
        dataListOf.addOnPropertyChangedCallback(new androidx.databinding.Observable.OnPropertyChangedCallback() {
            @Override
            public void onPropertyChanged(androidx.databinding.Observable sender, int propertyId) {
                onDataListChangedCallback(getDataList());
            }
        });
    }

    protected void onDataListChangedCallback(List<Attachment> dataList) {
        showAttEmptyHint.set(CollectionUtils.isEmpty(dataList));
    }

    public void setAttachmentAdapter(T attachmentAdapter) {
        this.attachmentAdapter = attachmentAdapter;
        this.attachmentAdapter .setAttachmentReviewVmBox(this);
    }

    public AttachmentAdapter getAttachmentAdapter() {
        return attachmentAdapter;
    }

    public ProtectedUnPeekLiveData<Attachment> getNotifyAttItemDataChangeLd() {
        return notifyAttItemDataChangeLd;
    }

    public int getNotifyAttItemDataChangeByIndex() {
        return getDataList().indexOf(notifyAttItemDataChangeLd.getValue());
    }

    /**
     * 初始化
     *
     * @param ids
     */
    public void setAttIds(Long[] ids) {
        this.ids = ids;
    }

    public void setAttList(List<Attachment> attachmentList) {
        getDataListSlcObserver().onNext(SlcEntity.getSlcEntityByData(attachmentList));
    }

    /**
     * 初始化
     *
     * @param ids
     */
    public void setAttIdsAndFind(Long[] ids) {
        setAttIds(ids);
        refresh();
    }

    /**
     * 使用storageFormList初始化
     *
     * @param storageFormList
     */
    public void setStorageFormList(@NonNull List<StorageForm> storageFormList) {
        this.storageFormList = storageFormList;
        refresh();
    }

    public List<StorageForm> getStorageFormList() {
        return storageFormList;
    }

    @Override
    public void refresh() {
        super.refresh();
        if (getAttachmentAdapter() != null) {
            getAttachmentAdapter().onRefresh(getDataListSlcObserver());
        }
    }

    @Override
    public void itemClick(int index, Attachment data) {
        if (data.isLocalBody()) {
            onPreview(data);
            return;
        }
        Progress progress = data.getProgress();
        if (data.isNetBody() && progress != null && (progress.status == Progress.WAITING || progress.status == Progress.LOADING)) {
            ToastUtils.showShort(R.string.app_label_please_wait_for_the_download_to_complete);
        } else {
            onDownload(data);
        }
    }

    protected void onPreview(Attachment attachment) {
        if (getAttachmentAdapter() != null) {
            getAttachmentAdapter().onPreview(attachment);
        }

    }

    public void onDownload(Attachment attachment) {
        if (getAttachmentAdapter() != null) {
            getAttachmentAdapter().onDownload(attachment);
        }
    }

    public void removeAtt(int index) {
        if (index > 0 && getDataList().size() > index) {
            getDataList().remove(index);
        }
    }

    public void removeAtt(Attachment attachment) {
        getDataList().remove(attachment);
        dataListOf.notifyChange();
    }

    public interface AttachmentAdapter {
        void setAttachmentReviewVmBox(AttachmentReviewVmBox<? extends AttachmentAdapter> attachmentVmBox);

        void onPreview(Attachment attachment);

        void onDownload(Attachment attachment);

        void onRefresh(SlcObserver<List<Attachment>> observer);

        void uploadAttachmentSingleDirectlyUnder(Attachment attachment);

        Observable<Attachment> uploadAttachmentSingle(Attachment attachment);

        Observable<Attachment> uploadAttachment();
    }
}